package com.zxymq.consumer.config;

import com.fasterxml.jackson.core.type.TypeReference;
import com.zxymq.consumer.domain.Order;
import com.zxymq.constants.MQBusinessConstants;
import com.zxymq.consumer.receiver.UsermodelMessageListenter;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 业务相关 RabbitMQ交换机、队列的配置类.定义交换机、key、queue并做好绑定。
 * Created by Administrator on 2018/1/10.
 */
@Configuration
@ComponentScan
public class BusQueueConsumerConfig {
    /**
     * 业务队列的监听
     *
     * @param connectionFactory             RabbitMQ连接工厂
     * @param bussesLetterListenerContainer 业务的监听者
     * @return 监听容器对象
     */
    @Bean
    public SimpleMessageListenerContainer bussesLetterListenerContainer(ConnectionFactory connectionFactory,
                                                                        UsermodelMessageListenter usermodelMessageListenter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        //container.setQueues(busQueue());
        container.setQueueNames(MQBusinessConstants.BUS_QUEUE);
        container.setExposeListenerChannel(true);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setMessageListener(usermodelMessageListenter);
        //消费端配置映射
        usermodelMessageListenter.setComplexGeneric(new TypeReference<List<Order>>(){});
        usermodelMessageListenter.setMaxConsumerCount(100);
        container.setPrefetchCount(100);
        return container;
    }
}
